﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Handrise.CDCable.Mobile.GISControl;
using Handrise.CDCable.Mobile.Business;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmGIS : FrmBase
    {
        private double scalenum; //缩放比例
        private string parentForm = "";
        private string parentInfo="";
        private bool enabletrack = false;

        public FrmGIS()
        {
            InitializeComponent();
            this.ttlbrhead.Text = ResourceImage.GIS;
            UGISmap.CacheStoragePath = AppLogic.GetLocalPath() + @"\mapcache";
            ibzoomout.UpImage = ResourceImage.缩小可点击;
            ibzoomout.DownImage = ResourceImage.缩小点击;
            ibzoomout.DisableImage = ResourceImage.缩小不可点击;
            Ibzoomin.UpImage = ResourceImage.放大可点击;
            Ibzoomin.DownImage = ResourceImage.放大点击;
            Ibzoomin.DisableImage = ResourceImage.放大不可点击;
            UGISmap.Getcatchmap();
            scalenum = UGISmap.Scale / 10;
            UGISmap.Scale = UGISmap.Scale=14000;


        }
        /// <summary>
        /// 获取轨迹点
        /// </summary>
        private void Gettrackpoint()
        {
            // GetuserGPSTrace
            GPSTraceService gpsts = new GPSTraceService();

            List<GPSTrace> GPSTraces = gpsts.GetuserdateGPSTrace(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")), Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")), AppLogic.LoginUser.UserName);
            if (GPSTraces.Count != 0)
            {

                PDGPS PDGPS = new PDGPS();


                foreach (GPSTrace GPSTrace in GPSTraces)
                {
                    PDGPS.DeviceGPSID = GPSTrace.GPSTraceID;
                    PDGPS.DeviceCode = GPSTrace.DeviceCode;
                    PDGPS.DeviceType = "";
                    PDGPS.LocalGPSInfo = PDGPS.LocalGPSInfo + ";" + GPSTrace.Latitude.ToString() + "," + GPSTrace.Longitude.ToString();
                    PDGPS.GPSInfo = PDGPS.GPSInfo + ";" + GPSTrace.Latitude.ToString() + "," + GPSTrace.Longitude.ToString();
                }
                PDGPS.Devicename = "轨迹";
                UGISmap.ListPoint.Add(PDGPS);
            }
            UGISmap.changepoint();
        }
        


        private void bcxunjian_OnButtonClick(object sender, int buttonIndex)
        {

        }

        private void bcxunjian_OnButtonClick_1(object sender, int buttonIndex)
        {
            switch (buttonIndex)
            {
                case 2:
                    FormManage.GotoNext("FrmMain", null, null, true);
                    break;
                case 1:
                    if (!enabletrack)
                    {
                        Gettrackpoint();
                        enabletrack = !enabletrack;
                    }
                    else
                    {
                        UGISmap.Getcatchmap();
                        Getpoint(parentInfo);
                        enabletrack = !enabletrack;
                    };
                    break;
                case 0:
                    // FormManage.GotoNext("FrmGISlayer", null,null , false);
                    FormManage.GotoNext("FrmGISlayer", new object[] { "FrmGIS", null }, null, false);
                    break;

            }
        }
        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            parentForm = (string)Parameter[0];
            parentInfo = (string)Parameter[1];
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void Ibzoomin_Click(object sender, EventArgs e)
        {
            UGISmap.Scale = UGISmap.Scale - UGISmap.Scale/5;
            UGISmap.showlayer();
        }

        private void ibzoomout_Click(object sender, EventArgs e)
        {

            UGISmap.Scale = UGISmap.Scale + UGISmap.Scale / 5;
            UGISmap.showlayer();

        }

        private void FrmGIS_Load(object sender, EventArgs e)
        {
            LoadData();
            Getpoint(parentInfo);

        }
        /// <summary>
        /// 获取设备点
        /// </summary>

        private void Getpoint(string wheresql)
        {
            PlanService planService = new PlanService();
            List<VPlanDevice> planDevices = new List<VPlanDevice>();
            PlanDeviceService planDeviceService = new PlanDeviceService();
            List<Plan> plans = planService.GetDefaultPlans(AppLogic.LoginUser.UserName);

            if (plans.Count != 0)
            {
                string sqlwhere = "";
                foreach (Plan plan in plans)
                {
                    sqlwhere = sqlwhere + " or (PlanID='" + plan.PlanID.ToString() + "')";
                }
                sqlwhere = sqlwhere.Substring(3);
                if (wheresql!= "")
                    if (wheresql != null)
                    {
                        sqlwhere ="("+ wheresql + ") and (" + sqlwhere+")";
                        UGISmap.clearpaintpont();
                    }
                planDevices = planDeviceService.GetsqlPlanDevice(sqlwhere);
                foreach (VPlanDevice pdevice in planDevices)
                {
                    {
                        if (!(UGISmap.ListPoint.Exists(delegate(PDGPS p) { return p.DeviceCode == pdevice.DeviceCode; })))
                        {
                            PDGPS PDGPS = new PDGPS();
                            PDGPS.DeviceGPSID = pdevice.DeviceID;
                            PDGPS.DeviceCode = pdevice.DeviceCode;
                            PDGPS.DeviceType = pdevice.DeviceType;
                            PDGPS.LocalGPSInfo = pdevice.LocalGPSInfo;
                            PDGPS.GPSInfo = pdevice.GPSInfo;
                            PDGPS.Devicename = pdevice.DeviceName;
                            UGISmap.ListPoint.Add(PDGPS);
                        }
                    }
                }
                UGISmap.changepoint();
            }
        }

        private void FrmGIS_Closed(object sender, EventArgs e)
        {
            UGISmap.closecatch();
        }

    }
}
